import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:http/http.dart' as http;

/// 消息详情页面
class LookeventPage extends StatefulWidget {
  final String imghttp;
  final String deviceName;

  const LookeventPage({super.key, required this.imghttp,required this.deviceName});

  @override
  State<StatefulWidget> createState() {
    return _LookeventPageState();
  }
}

class _LookeventPageState extends State<LookeventPage> {
  late String _imghttp;
  late String _deviceName;

  @override
  void initState() {
    super.initState();
    _imghttp = widget.imghttp;
    _deviceName = widget.deviceName;

    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Color(0xFFffffff),
      statusBarIconBrightness: Brightness.dark,
    ));
  }

  Future<bool> _onWillPop() async {
    // 退出横屏状态
    _exitFullScreen();
    return true;
  }

  void _toggleFullScreen() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
    ]);
  }

  void _exitFullScreen() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
  }

  Future<void> _saveImage() async {
    final response = await http.get(Uri.parse(_imghttp));
    final result = await ImageGallerySaver.saveImage(
      response.bodyBytes,
      quality: 100,
      name: "image_${DateTime.now().millisecondsSinceEpoch}",
    );
    if (result['isSuccess']) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('图片已保存到相册')),
      );
    } else {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('保存失败')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: Icon(Icons.arrow_back_ios),
            onPressed: () {
              _onWillPop();
              Navigator.of(context).pop();
            },
          ),
          automaticallyImplyLeading: false,
          title: Text(
            _deviceName,
            style: TextStyle(
              fontSize: 17,
              fontFamily: "PingFang SC",
              fontWeight: FontWeight.w600,
            ),
          ),
          centerTitle: true,
        ),
        body: Container(
          height: double.infinity,
          color: Color(0xFFF2F3F5),
          child: SafeArea(
            child: Column(
              children: [
                Stack(
                  children: [
                    Container(
                      width: double.infinity,
                      height: 230,
                      child: Image.network(
                        _imghttp,
                        fit: BoxFit.cover,
                      ),
                    ),
                    // Positioned(
                    //   right: 10,
                    //   bottom: 10,
                    //   child: IconButton(
                    //     icon: Icon(Icons.fullscreen, color: Colors.white),
                    //     onPressed: _toggleFullScreen,
                    //   ),
                    // ),

                  ],
                ),
                // SizedBox(height: 20),
                Container(
                  width: double.infinity, // 宽度撑满屏幕
                  color: Colors.black, // 背景设置为黑色
                  child: Padding(
                    padding: const EdgeInsets.symmetric(vertical: 10), // 上下各 20 的 padding
                    child: Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center, // 整体上下居中
                        crossAxisAlignment: CrossAxisAlignment.center, // 水平居中
                        children: [
                          InkWell(
                            onTap: _saveImage, // 点击事件
                            child: Icon(
                              Icons.download_outlined,
                              color: Colors.white, // 图标颜色为白色
                            ),
                            splashColor: Colors.transparent, // 去除点击水波纹效果
                            highlightColor: Colors.transparent, // 去除点击高亮效果
                          ),
                          Text(
                            '下载',
                            style: TextStyle(color: Colors.white), // 文字颜色为白色
                          ),
                        ],
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
      onWillPop: _onWillPop,
    );
  }
}